<?php

namespace addons\qingdongams\model;

use think\Exception;
use think\Model;
use traits\model\SoftDelete;

/**
 * 售后问题管理
 */
class Aftermarket Extends Model {
	use SoftDelete;

    // 表名,不含前缀
    protected $name = 'qingdongams_aftermarket';
	// 开启自动写入时间戳字段
	protected $autoWriteTimestamp = 'int';
	// 定义时间戳字段名
	protected $createTime = 'createtime';
	protected $updateTime = 'updatetime';
	protected $deleteTime = 'deletetime';

	public function getTypeIdsAttr($value){
	    return trim($value,',');
    }
    public function getImgAttr($value) {
	    if($value){
            return cdnurl($value, true);
        }
        return $value;
    }
    //获取创建时间
    public function getCreatetimeAttr($value) {
        return date('Y-m-d H:i:s', $value);
    }

	//创建售后问题
	public static function createAftermarket($params) {

		$staff = Staff::info();
		if (!empty($staff)) {
			$params['create_staff_id'] = $staff->id;
		}
		$customer = new self;
		// 调用当前模型对应的User验证器类进行数据验证
		$result = $customer->allowField(true)->validate(true)->save($params);
		$lastId = $customer->getLastInsID();
		if (false === $result) {
			// 验证失败 输出错误信息
			throw new Exception($customer->getError());
		}

		return $lastId;
	}


	/**
	 *修改售后问题
	 */
	public static function updateAftermarket($params) {

		$customer = new self;
		// 调用当前模型对应的User验证器类进行数据验证
		$result = $customer->save($params, ['id' => $params['id']]);
		if (false === $result) {
			// 验证失败 输出错误信息
			throw new Exception($customer->getError());
		}

		return true;
	}

	public function product(){
		return $this->belongsTo(Product::class,'id','product_ids');
	}

    public function staff() {
        return $this->belongsTo(Staff::class, 'create_staff_id', 'id')->field('id,name,img,post');
    }
}
